package com.collect.common.eventbus.configuration;

import com.collect.common.eventbus.annotation.EventBusListenerAnnotation;
import com.collect.common.eventbus.support.EventBusSupport;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @Author hky
 * @Description
 * @Date 2021/1/18
 */
public class EventBusAnnotationPostProcessor implements BeanPostProcessor {

    private final EventBusSupport eventBusSupport;

    public EventBusAnnotationPostProcessor(EventBusSupport eventBusFactory) {
        this.eventBusSupport = eventBusFactory;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> aClass = AopProxyUtils.ultimateTargetClass(bean);
        if(aClass.isAnnotationPresent(EventBusListenerAnnotation.class)){
            eventBusSupport.asyncRegister(bean);
        }
        return bean;
    }
}
